<?php  
namespace app\admin\controller;
// use think\Controller;
class Industry extends Base{
	public function index(){
		return view();
	}
	public function classify_list(){
		$arr=db('industry')->where('type=0')->paginate(5); 
		return view('',['arr'=>$arr]);
	}
	public function classify_add(){
		return view();
	}
	public function classify_insert(){
		$rst=db('industry')->insert($_POST);
		if($rst){
			$this->success('添加成功','classify_list');
		}else{
			$this->error('添加失败','classify_list');
		}
	}
	public function classify_edit(){
		if(db('industry')->update($_POST)){
			$this->success('修改成功','classify_list');
		}else{
			$this->error('修改失败','classify_list');
		}
	}
	public function classify_del($id){
		$arr=db('industry')->find($id);
		if(db('industry')->where('type','1')->where('typeclass',$arr['typeclass'])->find()){
			$this->error('该产品类别下含有产品，不可删除','classify_list');
		}
		if(db('industry')->delete($id)){
			$this->success('删除成功','classify_list');
		}else{
			$this->error('删除失败','classify_list');
		}
	}


	public function product_list(){
		$arr=db('industry')->where('type=1')->paginate(5);
		// halt($arr);
		return view('',['arr'=>$arr]);
	}
	public function product_add(){
		$arr=db('industry')->where('type=0')->select();
		// halt($arr);
		return view('',['arr'=>$arr]);
	}
	public function product_insert(){
		// halt($_POST);
		$file=request()->file('img');
		$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
		if($info){
			$img=$info->getSaveName();
		}else{
			echo $info->getError();	
		}
		$_POST['img']=$img;
		if(db('industry')->insert($_POST)){
			$this->success('添加成功','product_list');
		}else{
			$this->error('添加失败','product_list');
		}
	}
	public function product_del($id){
		$arr=db('industry')->find($id);
		if(db('industry')->delete($id)){
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img']);
			$this->success('删除成功','product_list');
		}else{
			$this->error('删除失败','product_list');
		}
	}
	public function product_edit(){
		$arr=db('industry')->where('type=0')->select();
		// halt($_GET);
		$old=db('industry')->find($_GET['id']);
		// halt($old);
		return view('',['arr'=>$arr,'old'=>$old]);
	}
	public function product_update(){
		$arr=db('industry')->find($_POST['id']);
		// halt($_FILES['img']);
		if($_FILES['img']['error']>0){
			$_POST['img']=$arr['img'];
		}else{
			unlink(ROOT_PATH.'public'.'/static/uploads/'.$arr['img']);
			$file=request()->file('img');
			$info=$file->move(ROOT_PATH.'public'.'/static/uploads');
			if($info){
				$_POST['img']=$info->getSaveName();
			}else{
				echo $info->getError();	
			}
		}
		// halt($_POST['img']);
		if(db('industry')->update($_POST)){
			$this->success('修改成功','product_list');
		}else{
			$this->error('修改失败','product_list');
		}
	}
}

